package com.store.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.store.configure.Result;
import com.store.entity.User;
import com.store.mapper.UserMapper;
import com.store.service.UserService;
import com.store.utils.IDGenerator;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Resource
    UserMapper userMapper;

    @Override
    public Result<User> updateUser(User user) {
        Result<User> result = new Result<>();
        try {
            if (StringUtils.isEmpty(user.getUserId())) {
                //如果ID为空，代表添加
                user.setUserId(IDGenerator.getUUID("USER"));
                userMapper.insert(user);
            } else {
                //如果ID不为空，代表修改
                userMapper.updateById(user);
            }
            result.setMessage("success");
        } catch (Exception e) {
            result.setMessage(e.getMessage());
        }
        return result;
    }

    /**
     * 删除用户--UserList.vue
     * @param user
     * @return
     */

    @Override
    public Result<User> delUser(User user) {
        Result<User> result = new Result<>();
        try {
            if (StringUtils.isEmpty(user.getUserId())) {
                result.setMessage("userId can't be empty");
                return result;
            }
            userMapper.deleteById(user.getUserId());
            result.setMessage("success");
        } catch (Exception e) {
            result.setMessage(e.getMessage());
        }
        return result;
    }

    @Override
    public Result<User> batchDelUser(User user) {
        Result<User> result = new Result<>();
        try {
            if (!StringUtils.isEmpty(user.getIds())) {
                String[] ids = user.getIds().split(",");
                for (String id : ids) {
                    userMapper.deleteById(id);
                }
            }
        } catch (Exception e) {
            result.setMessage(e.getMessage());
        }
        return result;
    }



    @Override
    public Result<User> queryUser(User user) {
        Result<User> result = new Result<>();
        try {
            QueryWrapper<User> wrapper = new QueryWrapper<>();
            //查询条件拼接，仅限单表查询可用
            if (!StringUtils.isEmpty(user.getUserAccount())) {
                wrapper.lambda().eq(User::getUserAccount, user.getUserAccount());
            }
            if (!StringUtils.isEmpty(user.getUserName())) {
                wrapper.lambda().like(User::getUserName, user.getUserName());
            }
            if (!StringUtils.isEmpty(user.getUserType())) {
                wrapper.lambda().eq(User::getUserType, user.getUserType());
            }

            List<User> users = userMapper.selectList(wrapper);
            result.setReturnList(users);
            result.setMessage("success");
        } catch (Exception e) {
            result.setMessage(e.getMessage());
        }
        return result;
    }

    /**
     * userList.vue--名字查询，分页
     * @param user
     * @return
     */

    @Override
    public Result<User> queryUserByPage(User user) {
        Result<User> result = new Result<>();

        try {
            if (user.getCurrent() == null || user.getSize() == null) {
                result.setMessage("current or size can't be null");
                return result;
            }

            QueryWrapper<User> wrapper = new QueryWrapper<>();
            //查询条件拼接，仅限单表查询可用
//            if (!StringUtils.isEmpty(user.getUserAccount())) {
//                wrapper.lambda().eq(User::getUserAccount, user.getUserAccount());
//            }
            if (!StringUtils.isEmpty(user.getUserName())) {
                wrapper.lambda().like(User::getUserName, user.getUserName());
            }
            if (!StringUtils.isEmpty(user.getUserType())) {
                wrapper.lambda().eq(User::getUserType, user.getUserType());
            }
            IPage<User> users = userMapper.selectPage(new Page<>(user.getCurrent(), user.getSize()), wrapper);
            result.setReturnPage(users);
            result.setMessage("success");
        } catch (Exception e) {
            result.setMessage(e.getMessage());
        }
        return result;
    }

    @Override
    public Result<User> queryUserById(User user) {
        Result<User> result = new Result<>();
        try {
            User returnUser = userMapper.selectById(user.getUserId());
            result.setReturnObj(returnUser);
            result.setMessage("success");
        } catch (Exception e) {
            result.setMessage(e.getMessage());
        }
        return result;
    }

    @Override
    public Result<User> register(User user) {
        Result<User> result = new Result<>();
        try {
            QueryWrapper<User> wrapper = new QueryWrapper<>();
            wrapper.lambda().eq(User::getUserAccount, user.getUserAccount());
            List<User> userList = userMapper.selectList(wrapper);
            if(userList.size() > 0){
                result.setMessage("该账号已存在");
                return result;
            }
            //暂定不能注册管理员
            user.setUserType("2");
            user.setUserStatus("1");
            userMapper.insert(user);
            result.setMessage("success");
        } catch (Exception e) {
            result.setMessage(e.getMessage());
        }
        return result;
    }

    @Override
    public Result<User> updatePassword(User user) {
        Result<User> result = new Result<>();
        try {
            QueryWrapper<User> wrapper = new QueryWrapper<>();
            wrapper.lambda().eq(User::getUserAccount, user.getUserAccount());
            wrapper.lambda().eq(User::getUserPassword, user.getOldPassword());
            List<User> userList = userMapper.selectList(wrapper);
            if(userList.isEmpty()){
                result.setMessage("旧密码输入错误，无法修改");
                return result;
            }
            userMapper.updateById(user);
            result.setMessage("success");
        } catch (Exception e) {
            result.setMessage(e.getMessage());
        }
        return result;
    }
}
